package com.youlai.boot.system.controller;

import com.youlai.boot.config.BatchUpdateRequest;
import com.youlai.boot.system.service.TaskTableV2Service;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.TaskTableForm;
import com.youlai.boot.system.model.query.TaskTableQuery;
import com.youlai.boot.system.model.vo.TaskTableVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Map;

/**
 * TaskTable前端控制层
 *
 * @author Gwd
 * @since 2025-03-07 17:03
 */
@Tag(name = "TaskTable接口")
@RestController
@RequestMapping("/api/v2/taskTables")
@RequiredArgsConstructor
public class TaskTableV2Controller  {

    private final TaskTableV2Service taskTableV2Service;

    @Operation(summary = "TaskTable分页列表")
    @GetMapping("/page")
    public PageResult<TaskTableVO> getTaskTablePage(TaskTableQuery queryParams ) {
        IPage<TaskTableVO> result = taskTableV2Service.getTaskTablePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增TaskTable")
    @PostMapping
    public Result<Void> saveTaskTable(@RequestBody @Valid TaskTableForm formData ) {
        boolean result = taskTableV2Service.saveTaskTable(formData);
        return Result.judge(result);
    }


    @Operation(summary = "获取TaskTable表单数据")
    @GetMapping("/{id}/form")
    public Result<TaskTableForm> getTaskTableForm(
            @Parameter(description = "TaskTableID") @PathVariable Long id
    ) {
        TaskTableForm formData = taskTableV2Service.getTaskTableFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改TaskTable")
    @PutMapping(value = "/{id}")
    public Result<Void> updateTaskTable(
            @Parameter(description = "TaskTableID") @PathVariable Long id,
            @RequestBody @Validated TaskTableForm formData
    ) {
        boolean result = taskTableV2Service.updateTaskTable(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除TaskTable")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteTaskTables(
            @Parameter(description = "TaskTableID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = taskTableV2Service.deleteTaskTables(ids);
        return Result.judge(result);
    }

    /**
     *
     * @return
     */

    @GetMapping("conter")
    @Operation(summary = "查看所有任务")
    public Result<Integer> getAllTasktable(){
        Integer conter = taskTableV2Service.getTotalDoctors();
            return Result.success(conter);
    }

    /**
     * 归档接口
     *
     * @param archive
     * @return
     */
    @PutMapping("/{id}/archive")
    public Result<Void> updateArchiveStatus(@PathVariable Long id, @RequestParam Integer archive) {
        if (archive != 0 && archive != 1) {
            return Result.error("nvalid archive status. Must be 0 or 1.");
        }
        boolean result = taskTableV2Service.updateArchive(id, archive);
        if (result) {
            return Result.success("Update successful.");
        } else {
            return Result.error("Failed to update archive status.");
        }
    }

    /**
     * 批量归档接口
     */
    @PutMapping("/batch")
    public Result<Void> batchUpdateArchiveStatus(@RequestBody BatchUpdateRequest request) {
        if (request.getId() == null || request.getId().isEmpty()
                || (request.getArchive() != 0 && request.getArchive() != 1)) {
            return Result.error("Invalid input.");
        }

        boolean result = taskTableV2Service.batchUpdateArchiveStatus(request.getId(), request.getArchive());
        if (result) {
            return Result.success("Batch update successful.");
        } else {
            return Result.error("Failed to batch update archive status.");
        }
    }

//    /**
//     * 根据归档为1查询
//     *
//     * @return
//     */
//    @GetMapping("/archivetasks")
//    public PageResult<TaskTableVO> getArchiveTasks(TaskTableQuery queryParams) {
//        IPage<TaskTableVO> result = taskTableV2Service.getTaskTablePage(queryParams);
//        return PageResult.success(result);
////        return Result.success(tasktableService.getArchiveTasks());
//    }

    /**
     * 获取任务完成情况统计数据
     *
     * @return
     */
    @GetMapping("/completion-ratio")
    public Result<List<Map<String, Object>>> getTaskCompletionRatio() {
        return Result.success(taskTableV2Service.getTaskCompletionCount());
    }
}
